import type { IAction, IState } from '@/types/stores/user-store';
import { create } from 'zustand';
import { TOKEN_STORAGE_KEY } from '@/common/constants';

const storageToken =
  sessionStorage.getItem(TOKEN_STORAGE_KEY) || localStorage.getItem(TOKEN_STORAGE_KEY) || '123';
const useUserStore = create<IAction & IState>((set) => ({
  userInfo: null,
  accessToken: storageToken,
  setState: (state) => set(() => ({ ...state })),
  logout: () => {
    set({
      userInfo: null,
      accessToken: null
    });
    sessionStorage.removeItem(TOKEN_STORAGE_KEY);
    localStorage.removeItem(TOKEN_STORAGE_KEY);
  }
}));

export default useUserStore;
